<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/exercise.jpg" />
      </div>
      <span> 运动健康平台 </span>
      <el-button type="info" plain @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--  unique-opened=true 是否只保持一个子菜单的展开 -->
        <div class="toggle-button" @click="changeCollapse()">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#d3dce6"
          text-color="black"
          active-text-color="rgb(0, 204, 238)"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="m.id + ''" v-for="m in menuList" :key="m.id">
            <template slot="title">
              <i :class="iconList[m.id]"></i>
              <span>{{ m.title }}</span>
            </template>
            <!-- //二级菜单 -->

            <el-menu-item-group>
              <el-menu-item
                :index="s.path + ''"
                v-for="s in m.sMenus"
                :key="s.id" 
                @click="saveActivePath(s.path)" 
              >
                <i :class="iconList[s.id]"></i>
                <span>{{ s.title }}</span>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>


<script>
export default {
  name: "Home",
  data() {
    return {
      menuList: [],
      isCollapse: false,
      iconList: {
        100: "iconfont icon-quanxianguanli",
        101: "iconfont icon-yonghuliebiao",
        102: "iconfont icon-quanxian",
        103: "iconfont icon-yundong1",
        104: "iconfont icon-shangpin",
        200: "iconfont icon-yundong",
        201: "iconfont icon-dianlikepu",
        202: "iconfont icon-qialuli",
        203: "iconfont icon-shiwu",
      },
      activePath:"/weclome",
    };
  },
  created() {
    this.getAllMenuList();
    this.activePath=window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    getAllMenuList() {
      this.$http.post("mMenu").then((response) => {
        console.log(response.data);
        console.log(response.data.data.mMenu);
        if (response.data.data.flag == 0) {
          this.$message.error(response.data.data.msg);
        } else {
          this.menuList = response.data.data.mMenu;
        }
      });
    },
    changeCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveActivePath(activePaths){
      window.sessionStorage.setItem("activePath",activePaths);
      this.activePath=activePaths;
    }
  },
};
</script>
 
<style lang="less" scoped>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  display: flex;
  align-items: center;
  line-height: 60px;
  justify-content: space-between;
  padding-left: 0%;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  } 
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e9eef3;
  color: #333;
}
.home-container {
  height: 100%;
}

img {
  height: 60px;
  width: 100px;
}
.el-submenu {
  i {
    font-size: 20px;
  }
  span {
    font-size: 20px;
  }
  .el-menu-item {
    font-size: 17px;
  }
}

//|||按钮
.toggle-button {
  background-color: rgb(184, 185, 185);
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer; //显示小手
}
</style>
